package parking.abcast;

import java.util.ArrayList;

/**
 * Class used by the initiator of a message. It is useful in keeping a dynamic table of all the received
 * clock.idSender sent by all the members of the group for a given message.
 * The initiator can thus choose the maximum amongst the received values and afterwards diffuse this chosen
 * value to the members of the group.
 * @author Sophie and Ronnie
 * @date 9 November 2013
 */
public class RetourAck {
	private String idMessage;			//id of the original message
	private int nbClients;				//the number of clients within the group
	private ArrayList<Float> retourAck;	//dynamic table for storing the received clock.idSender values
	
	/**
	 * Constructor for the RetourAck class
	 * It also enables the memory allocation of the dynamic table for storing the received clock.idSender values 
	 * @param idMessage : identity of the original message
	 * @param nbClients : number of clients within the group
	 */
	public RetourAck(String idMessage, int nbClients) {
		this.idMessage = idMessage;
		this.nbClients = nbClients;
		this.retourAck = new ArrayList<Float>();
	}

	/**
	 * @return the original id of the message. This id is unique in the system
	 */
	public String getIdMessage() {
		return idMessage;
	}

	/**
	 * @return a table with all the received clock.idSender values
	 */
	public ArrayList<Float> getRetourAck() {
		return retourAck;
	}

	/**
	 * @return the number of clients within the group
	 */
	public int getNbClients() {
		return nbClients;
	}
	
	/**
	 * @return for a given id of a message, the table associated where all the received clock.idSender values
	 * are stored. Used mainly for debugging purposes
	 */
	public String toString() {
		String retour = "";
		int i;
		
		retour = "-- Identifiant message : " + this.idMessage + " | " + "Nombre de clients : " + this.nbClients + "\n";
		retour += "-- ";
		
		for (i = 0; i < this.retourAck.size() - 1; i++) {
			retour += this.retourAck.get(i).floatValue() + " | ";
		}
		
		retour += this.retourAck.get(i).floatValue();
		
		return retour;
	}
}
